package com.hqq.behaviour.observer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Weather
 * Created by heqianqian on 2017/6/17.
 */
public class Weather {

    private static final Logger LOGGER = LoggerFactory.getLogger(Weather.class);

    private List<WeatherObserver> observerList;

    private WeatherType weatherType;

    public Weather() {
        observerList = new ArrayList<>();
        weatherType = WeatherType.SUNNY;
    }

    public void addObserver(WeatherObserver weatherObserver) {
        observerList.add(weatherObserver);
    }

    public void removeObserver(WeatherObserver weatherObserver) {
        observerList.remove(weatherObserver);
    }

    public void timePassed() {
        WeatherType[] enumValues = WeatherType.values();
        weatherType = enumValues[(weatherType.ordinal() + 1) % enumValues.length];
        LOGGER.info("The weather changed to {}.", weatherType);
        notifyObservers();
    }

    private void notifyObservers() {
        for (WeatherObserver observer : observerList) {
            observer.update(weatherType);
        }
    }

}
